﻿using Player;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerRoll : PlayerBehavior, IStateBase
{

    Rigidbody2D rigidbody;

    Vector2 rollDir;

    float rollSpeed;
    float rollSpeedDropMultiplier = 5.0f;
    float rollSpeedMinimun = 5f;

    bool isRollBack = false;

    Animator anim;

    public PlayerRoll(PlayerBody player) : base(player)
    {
        rigidbody = Player.GetComponent<Rigidbody2D>();
        anim = Player.GetComponentInChildren<Animator>();
    }

    public void GetState()
    {
        throw new System.NotImplementedException();
    }

    public void Interrupt()
    {
        throw new System.NotImplementedException();
    }

    public void OnEnter()
    {
        
        rollSpeed = Player.PlayerData.RollSpeed;
        
        Player.PlayerStatus.isRoll = true;
       // throw new System.NotImplementedException();
    }

    public void OnExit()
    {
        rigidbody.velocity = Vector2.zero;
        Player.PlayerStatus.isRoll = false;
        anim.SetFloat("RollSpeed", 0);
        // throw new System.NotImplementedException();
    }

    public void OnFixedUpdate()
    {
        
        rigidbody.velocity = rollSpeed * rollDir * Player.PlayerData.TimeScale;
        if (rollDir.y != 0)
        {
            isRollBack = true;
        }
        else
            isRollBack = false;
        anim.SetFloat("RollSpeed", rollSpeed);
        anim.SetBool("RollBack", isRollBack);
        //throw new System.NotImplementedException();
    }

    public void OnUpdate(params object[] obj)
    {
        if (rollSpeed < rollSpeedMinimun)
            OnExit();

        Vector2 dir = (Vector2)obj[0];
        rollDir = dir;
        rollSpeed -= rollSpeed * rollSpeedDropMultiplier * Time.deltaTime * Player.PlayerData.TimeScale;      

        //var mousePos = InputRead.GetID(Player.ID).AimPosition;
        //float angle = Vector3.Angle(mousePos, dir);

        //if(angle > 90)
        //{
        //    Player.transform.eulerAngles = new Vector3(Player.transform.eulerAngles.x, 
        //        Player.transform.eulerAngles.y+180, Player.transform.eulerAngles.z);
        //}
    }


}
